<template>
  <div class="new">
    <van-overlay :show="overlayShow" :z-index="1000" style="background-color: #ffffff">
      <div style="display: flex;align-items: center;justify-content: center;height: 100%;">
        <van-loading type="spinner" color="#1989fa">请稍候...</van-loading>
      </div>
    </van-overlay>

    <div>
      <van-collapse v-model="activeNames">
        <van-collapse-item size="large" v-for="item in jsondata.head" :key="item.grouptitle" :name="item.grouptitle" :title="item.grouptitle">
          <van-cell-group style="border:0px;">
            <div style="" v-for="itemdata in item.groupvalue" :key="itemdata.ID">
              <idatetime ishead :itemdata="itemdata" v-if="itemdata.sUIType == 'datetime'" :bzjsrqField.sync="bzjsrqField" :xzksrqField.sync="xzksrqField" :xzjsrqField.sync="xzjsrqField" />
              <icode ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'code'" />
              <ienum ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'enum'" :jsondata="jsondata" :idobject="idobject" />
              <iref ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'ref'" :clickmodel.sync="clickmodel" :clickfrom.sync="clickfrom" :clickindexName.sync="clickindexName" :clickindex.sync="clickindex" :jsondata="jsondata" :activeNames="activeNames" />
              <ibool ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'bool'" :jsondata="jsondata" :idobject="idobject" />
              <ifile ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'file'" :jsondata="jsondata" />
              <ipicture ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'picture'" :jsondata="jsondata" />
              <istring ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'string'" :jsondata="jsondata" :idobject="idobject" />
              <imap ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'map'" :jsondata="jsondata" :item="item" />
              <istring ishead :itemdata="itemdata" v-else :jsondata="jsondata" :idobject="idobject" />
            </div>
          </van-cell-group>
        </van-collapse-item>
      </van-collapse>
    </div>

    <!-- 单独处理的部分 -->
    <div class="customer-info">
      <p><span>*&nbsp;</span>客户信息：</p>
      <button class="add-btn" @click="addCustomer()">添加</button>
    </div>
    <div v-if="selectedCustomers && selectedCustomers.length > 0" class="ulcontainer">
      <ul class="customer-list">
        <li :key="index" class="customer-item" :id="index" v-for="(item, index) in selectedCustomers">
          <img :src="require('@/assets/bhead_whjm.png')" alt="公司logo">
          <div class="customer-details">
            <p class="title">{{ item.sname }}</p>
            <p>{{ item.saddress }}</p>
          </div>
          <!--        <button class="delete-btn" @click="delLi(index)" >X</button>-->
          <van-icon class="delete-btn" @click="delLi(index)" name="cross" />

        </li>
      </ul>
    </div>
    <div v-else class="empty-state">
      <svg width="64" height="41" viewBox="0 0 64 41" xmlns="http://www.w3.org/2000/svg">
        <g fill="none" fill-rule="evenodd" opacity=".5">
          <path d="M0 32h64v9H0z" fill="#F2F3F5" />
          <path d="M10 37h44v4H10z" fill="#FFF" />
          <g fill="#E0E0E0">
            <path d="M8 25h48v6H8zM10 0h44v22H10z" />
          </g>
          <path d="M19 10h10v3H19z" fill="#FFF" />
          <path d="M19 16h26v3H19z" fill="#F2F3F5" />
        </g>
      </svg>
      <p>暂无数据</p>
    </div>

    <van-tabbar v-model="active" safe-area-inset-bottom>
      <van-tabbar-item v-if="this.beforeSave && !this.afterSaveSucceed" @click="save">保存</van-tabbar-item>
      <van-tabbar-item v-if="isflow && this.beforeSave && !this.afterSaveSucceed" @click="savesubmit">保存并提交</van-tabbar-item>
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed" @click="submit">提交</van-tabbar-item>
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed" @click="audit">审核</van-tabbar-item>
      <!--      <van-tabbar-item v-if="this.idobject == 'arvouch'" @click="saveAudit">保存并审核</van-tabbar-item>-->
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import {
  Search,
  List,
  Cell,
  PullRefresh,
  Toast,
  Tabbar,
  TabbarItem,
  Popup,
  DatetimePicker,
  ActionSheet,
  Tab,
  Tabs,
  Collapse,
  CollapseItem,
  CheckboxGroup,
  Checkbox,
  Uploader,
  Overlay,
  Loading,
  Rate,
  NumberKeyboard,
  Dialog
} from 'vant'
import qs from 'qs'
import useInput from '@/utils/useinput'
import * as dd from 'dingtalk-jsapi'
Vue.use(useInput).use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(DatetimePicker).use(ActionSheet).use(Tab).use(Tabs).use(Collapse).use(CollapseItem).use(CheckboxGroup).use(Checkbox).use(Uploader).use(Overlay).use(Loading).use(Rate).use(NumberKeyboard)
export default {
  name: 'bflxnew',
  data () {
    let _this = this
    return {
      idobject: this.$route.query.idobject,
      idtemplate: this.$route.query.idtemplate,
      selecteditems: this.$route.query.selecteditems,
      jsondata: {},
      index: 0,
      active: 0,
      activeNames: [],
      activeBodyNams: [''],
      isLoad: false,
      conditionValHidden: '',
      clickmodel: {},
      clickfrom: '',
      clickindexName: '',
      clickindex: 0,
      isDisable: false,
      overlayShow: true,
      notemessage: '',
      isflow: false,
      bzjsrqField: '',
      xzksrqField: '',
      xzjsrqField: '',
      solrLoading: true,
      solrFinished: false,
      solrList: [],
      solrListDivStyle: {
        display: 'none'
      },
      /* 用明细的account字段标识所需的参数 */
      detailAccount: '',
      refCustomers: [],
      beforeSave: true,
      afterSaveSucceed: false
    }
  },
  created: function () {
    //console.log('created......')
    let _this = this
    document.title = this.$route.query.stitle
    //console.log('document.title', document.title)
    if (dd.env.platform !== 'notInDingTalk') {
      dd.ready(function () {
        dd.biz.navigation.setTitle({
          title: _this.$route.query.stitle
        })
      })
    }
  },
  computed: {
    selectedCustomers () {
      return this.refCustomers || []
    }
  },
  mounted: function () {
    //console.log('this.refCustomers', this.refCustomers)
    //console.log('start created')
    //console.log('idobject', this.idobject)
    if (!this.isLoad) {
      let data = {
        idtemplate: this.idtemplate
      }
      let url = '/api/' + this.idobject + '.new.do'
      this.axios({
        method: 'post',
        url: url,
        data: qs.stringify(data)
      }).then(res => {
        //console.log('res:', res)
        this.overlayShow = false
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          }
        } else {
          this.notemessage = res.data.notemessage
          this.isflow = res.data.isflow
          this.jsondata = res.data.jsondata
          for (let i = 0; i < res.data.jsondata.head.length; i++) {
            let head = res.data.jsondata.head[i]
            this.activeNames.push(head.grouptitle)
            //console.log('head...' + head)
          }

          this.saveDetailAccount()
          //console.log('this.detailAccount', this.detailAccount)

          //console.log('this.selecteditems', this.selecteditems)
          // 如果从客户列表过来的,设置refCustomers
          if (this.selecteditems && this.selecteditems.length > 0) {
            this.refCustomers = this.selecteditems
          }

          //console.log(this.jsondata)
          this.isLoad = true
        }
      }).catch(res => {
        this.reserror(res)
      })
    }
  },
  activated () {
    //console.log('this.$store.state.ref_arr', this.$store.state.ref_arr)
    //console.log('this.refCustomers', this.refCustomers)
    if (this.$store.state.ref_arr) {
      //console.log('this.$store.state.ref_arr', this.$store.state.ref_arr)
      if (!this.refCustomers && this.refCustomers.length <= 0) {
        this.refCustomers = this.$store.state.ref_arr
        return
      }
      const newArr = this.$store.state.ref_arr.filter(itemA => {
        return this.refCustomers.every(itemB => {
          return itemB.id !== itemA.id
        })
      })
      this.refCustomers = this.refCustomers.concat(newArr)
    }

    let a = { aa: 'aaa' }
    //console.log('a1', a)
    //console.log('clickmodel1', this.clickmodel)
    //console.log('clickindex1', this.clickindex)
    //console.log('clickindexName1', this.clickindexName)
    //console.log('clickfrom1', this.clickfrom)
    if (this.$store.state.ref_show !== '') {
      this.clickmodel.value = this.$store.state.ref_show
      this.clickmodel.valuehidden = this.$store.state.ref_id
      this.clickmodel.errormsg = ''
      let fieldmap = this.clickmodel.fieldmap
      //console.log('fieldmap', fieldmap)
      if (fieldmap !== undefined && fieldmap.length > 0) {
        let arrFieldmap = fieldmap.split(';')
        let item = this.$store.state.ref_item
        if (this.clickfrom === 'headdata') {
          a.aa = 'bbb'
          for (let groupindex = 0; groupindex < this.jsondata.head.length; groupindex++) {
            let itemgroup = this.jsondata.head[groupindex]
            for (let itemindex = 0; itemindex < itemgroup.groupvalue.length; itemindex++) {
              let itemdata = itemgroup.groupvalue[itemindex]
              for (let fieldmapindex in arrFieldmap) {
                let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
                if (this.$store.state.ref_field === arrFieldMapKey[1]) {
                  continue
                }
                if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
                  continue
                }
                if (itemdata.sField === arrFieldMapKey[1]) {
                  if (itemdata.sUIType === 'ref') {
                    itemdata.value = item[arrFieldMapKey[0] + '_ref']
                    itemdata.valuehidden = item[arrFieldMapKey[0]]
                  } else if (itemdata.sUIType === 'enum') {
                    itemdata.value = item[arrFieldMapKey[0] + '_enum']
                    itemdata.valuehidden = item[arrFieldMapKey[0]]
                  } else {
                    itemdata.value = item[arrFieldMapKey[0]]
                  }
                }
              }
            }
          }
        } else if (this.clickfrom === 'bodydata') {
          a.aa = 'ccc'
          for (let itemindex in this.jsondata.bodydata[this.clickindexName][this.clickindex]) {
            let itemdata = this.jsondata.bodydata[this.clickindexName][this.clickindex][itemindex]
            for (let fieldmapindex in arrFieldmap) {
              let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
              if (this.$store.state.ref_field === arrFieldMapKey[1]) {
                continue
              }
              if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
                continue
              }
              if (itemdata.sField === arrFieldMapKey[1]) {
                if (itemdata.sUIType === 'ref') {
                  itemdata.value = item[arrFieldMapKey[0] + '_ref']
                  itemdata.valuehidden = item[arrFieldMapKey[0]]
                } else if (itemdata.sUIType === 'enum') {
                  itemdata.value = item[arrFieldMapKey[0] + '_enum']
                  itemdata.valuehidden = item[arrFieldMapKey[0]]
                } else {
                  itemdata.value = item[arrFieldMapKey[0]]
                }
              }
            }
          }
        }
      }
      //console.log('a', a)
      //console.log('clickmodel', this.clickmodel)
      //console.log('clickindex', this.clickindex)
      //console.log('clickindexName', this.clickindexName)
      //console.log('clickfrom', this.clickfrom)
      this.clickmodel = {}
      this.clickindex = 0
      this.clickindexName = ''
      this.clickfrom = ''
      this.$store.commit('clearRef')
    }
  },
  methods: {
    addCustomer () {
      this.openRefMulti(this.detailAccount, 'headdata', '', 0)
    },
    openRefMulti (datamodel, datafrom, indexName, index) {
      //console.log('datamodel', datamodel)
      this.clickmodel = datamodel
      this.clickfrom = datafrom
      this.clickindexName = indexName
      this.clickindex = index
      this.conditionValHidden = ''
      let inview = datamodel.inview
      //console.log('inview', inview)
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  //console.log('item', item)
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                let item = this.jsondata.bodydata[indexName][index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            this.conditionValHidden += inviewdatas[0] + ':in:;'
          }
        }
      }
      //console.log('conditionValHidden', this.conditionValHidden)
      //console.log('datamodel....', datamodel)
      this.$router.push({
        name: 'refmulti4account',
        params: {
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname
        }
      })
    },
    saveDetailAccount () {
      for (let indexName in this.jsondata.body) {
        for (let i = 0; i < this.jsondata.body[indexName].length; i++) {
          let item = this.jsondata.bodydata[indexName][i]
          for (let itemindex in item) {
            let itemdata = item[itemindex]
            if (itemdata.sField === 'idaccount') {
              this.detailAccount = itemdata
              return
            }
          }
        }
      }
    },
    delLi (indexToRemove) {
      this.refCustomers.splice(indexToRemove, 1)
      Toast('删除成功!')
    },
    save () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }

        //console.log('jsondata', this.jsondata)
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          Toast('请检查必输项!')
          return
        }

        if (this.refCustomers.length == 0) {
          this.isDisable = false
          this.overlayShow = false
          Toast('请选择客户信息!')
          return
        }
        data.extrainfo = this.refCustomers.map(item => { return `${item.sname}` }).join(';')
        data.khs = this.refCustomers.length
        data.customerids = this.refCustomers.map(item => { return `${item.id}` }).join(';')
        data.customeraddr = this.refCustomers.map(item => { return `${item.label}` }).join(';')
        //console.log('data.......', data)
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.refCustomers = []
            Toast('保存成功')
            if (this.$route.query.newflag === 'refnew') {
              this.$store.commit('refReBack', { ref_show: 'true' })
              this.$router.go(-1)
              return
            }
            this.$router.push({ name: 'bflxlist', query: { idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    submit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.flowsubmit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.beforeSave = false
            this.afterSaveSucceed = true
            Toast('提交成功')
            if (this.$route.query.newflag === 'refnew') {
              this.$store.commit('refReBack', { ref_show: 'true' })
              this.$router.go(-1)
              return
            }
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    savesubmit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.isDisable = true
            this.overlayShow = true
            this.beforeSave = false
            this.afterSaveSucceed = true
            Toast('保存成功...开始提交...')
            // 发起提交请求开始 --
            this.axios({
              method: 'post',
              url: '/api/' + this.idobject + '.flowsubmit.do',
              data: qs.stringify(data)
            }).then(res => {
              this.isDisable = false
              this.overlayShow = false
              if (res.data.flag === '0') {
                Toast('提交成功')
                if (this.$route.query.newflag === 'refnew') {
                  this.$store.commit('refReBack', { ref_show: 'true' })
                  this.$router.go(-1)
                  return
                }
                this.$router.push({ name: 'list', query: { idobject: this.idobject } })
              } else {
                Toast(res.data.des)
                if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                  this.loginout()
                }
              }
              //console.log(res)
            }).catch(res => {
              this.isDisable = false
              this.overlayShow = false
              //console.log(res)
            })

            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    saveAudit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let i = 0; i < this.jsondata.bodydata.length; i++) {
            let item = this.jsondata.bodydata[i]
            for (let itemindex in item) {
              let itemdata = item[itemindex]
              if (itemdata.sUIType === 'ref') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
              } else if (itemdata.sUIType === 'enum') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
              } else {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsaveaudit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            Toast('保存并审核成功')
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    addRow (indexName) {
      let bodycopy = JSON.parse(JSON.stringify(this.jsondata.body[indexName]))
      this.jsondata.bodydata[indexName].push(bodycopy)
    },
    delRow (indexName, index) {
      if (this.jsondata.bodydata[indexName].length === 1) {
        Toast('至少保留一条表体行，不能删除！')
        return
      }

      Dialog.confirm({
        title: '',
        message: '你确定要删除' + indexName + ' ' + (index + 1) + '吗?'
      })
        .then(() => {
          // on confirm
          this.jsondata.bodydata[indexName].splice(index, 1)
        })
        .catch(() => {
          // on cancel
        })
    },
    toggle (index) {
      this.$refs.checkboxes[index].toggle()
    }
  }
}
</script>

<style scoped>
.fieldgroup_title {
  text-align: left;
  margin: 0px;
  padding: 25px 15px 15px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
}

div.customer-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.customer-info>p>span {
  color: red;
}

.ulcontainer {
  overflow-y: auto;
  margin-bottom: 70px;
}

.add-btn {
  color: rgba(39, 107, 202, 1);
  cursor: pointer;
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 5px 15px;
  background-color: #eee;
}

.customer-list {
  list-style-type: none;
  padding: 0;
}

.customer-item {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #eee;
  padding: 10px 20px;
  margin-bottom: 10px;
}

.customer-item :nth-child(1) {
  marigin-top: 5px;
}

.customer-item img {
  width: 40px;
  height: 40px;
  margin-right: 10px;
  min-width: 40px;
}

.customer-details {
  flex-grow: 1;
}

.customer-details .title {
  margin: 0;
  font-size: 16px;
  color: black;
}

.customer-details p {
  margin: 5px 0 0;
  font-size: 14px;
  color: #666;
}

.delete-btn {
  background: none;
  border: none;
  color: rgba(247, 123, 70, 1);
  cursor: pointer;
  align-self: center;
}

.input-group {
  margin-top: 15px;
  padding: 0 20px;
}

.input-group div {
  margin-top: 2px;
}

.input-group textarea {
  width: 100%;
  height: 120px;
  border: 1px solid #eee;
}

[class*=van-hairline]::after {
  border: none
}

.van-hairline--top-bottom::after,
.van-hairline-unset--top-bottom::after {
  border: none
}

.empty-state {
  text-align: center;
  padding: 50px 0;
  color: #999;
}

.empty-state svg {
  margin-bottom: 10px;
}

.empty-state p {
  font-size: 16px;
  color: #999;
}

.van-tabbar {
  display: flex;
  justify-content: center;
  padding-bottom: 15px !important;
}

.van-tabbar .van-tabbar-item {
  color: white;
  background-color: #5790df !important;
  width: 80% !important;
  flex: 0 0 80% !important;
  font-size: 14px;
  line-height: 14px;
}
</style>
